package unei;

import java.util.Date;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

public class GenerateUtils {
	/**
	 * 
	 * 方法用途: 创建ID<br>
	 * @param codePrefix 前缀,可为空
	 * @param length 后缀数字长度, 0不追加
	 * @return
	 */
	public synchronized static String getCode(String codePrefix, int length) {
		StringBuffer stringBuffer = new StringBuffer();
		if(null != codePrefix){
			stringBuffer.append(codePrefix);
		}
		
		stringBuffer.append(DateFormatUtils.format(new Date(), "yyMMddHHmmss"));
		
		String randomString = RandomStringUtils.randomNumeric(length);
		stringBuffer.append(randomString);
		
		return stringBuffer.toString();
	}
	
	/**
	 * 
	 * 方法用途: 创建ID<br>
	 * @param codePrefix 前缀,可为空
	 * @param length 后缀数字长度, 0不追加
	 * @return
	 */
	public static String getCodeNoSycn(String codePrefix, int length) {
		StringBuffer stringBuffer = new StringBuffer();
		if(null != codePrefix){
			stringBuffer.append(codePrefix);
		}
		
		stringBuffer.append(DateFormatUtils.format(new Date(), "yyMMddHHmmss"));
		
		String randomString = RandomStringUtils.randomNumeric(length);
		stringBuffer.append(randomString);
		
		return stringBuffer.toString();
	}
}
